package cn.yikaojiuguo.student.ui.global;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.app.YiKaoApplication;
import cn.yikaojiuguo.student.support.simple.ArgsConstants;
import cn.yikaojiuguo.student.support.utils.LogUtil;
import cn.yikaojiuguo.student.ui.base.BaseActivity;
import cn.yikaojiuguo.student.ui.base.BaseFragment;

public class HomeActivity extends BaseActivity implements View.OnClickListener{

    //提问
    @Bind(R.id.ll_ask)
    LinearLayout mLl_ask;
    @Bind(R.id.iv_ask)
    ImageView mIv_ask;
    @Bind(R.id.tv_ask)
    TextView mTv_ask;

    //课程
    @Bind(R.id.ll_course)
    LinearLayout mLl_course;
    @Bind(R.id.iv_course)
    ImageView mIv_course;
    @Bind(R.id.tv_course)
    TextView mTv_course;

    //我的
    @Bind(R.id.ll_my)
    LinearLayout mLl_my;
    @Bind(R.id.iv_my)
    ImageView mIv_my;
    @Bind(R.id.tv_my)
    TextView mTv_my;

    private List<ImageView> mIvList = new ArrayList<ImageView>(HomeFragmentFactory.FRAGMENT_COUNT);
    private List<TextView> mTvList = new ArrayList<TextView>(HomeFragmentFactory.FRAGMENT_COUNT);
    private int[] mSelectedImg = {R.drawable.home_ask_selected,R.drawable.home_course_selected,R.drawable.home_my_selected};
    private int[] mUnSelectedImg = {R.drawable.home_ask,R.drawable.home_course,R.drawable.home_my};

    private FragmentManager fragmentManager;
    //当前TAB的索引
    private int mCurTab;

    public static Intent newIntent(int tabIndex) {
        Intent intent = new Intent(YiKaoApplication.getInstance(),HomeActivity.class);
        intent.putExtra(ArgsConstants.INDEX, tabIndex);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        if(savedInstanceState == null) {
            LogUtil.showInfoLog("HomeActivity onCreate","savedInstanceState = null");
        } else {
            LogUtil.showInfoLog("HomeActivity onCreate","savedInstanceState != null");
        }
        ButterKnife.bind(this);
        initEvents();
    }

    private void initEvents() {
        mIvList.add(mIv_ask);
        mIvList.add(mIv_course);
        mIvList.add(mIv_my);
        mTvList.add(mTv_ask);
        mTvList.add(mTv_course);
        mTvList.add(mTv_my);
        fragmentManager = getSupportFragmentManager();
        mCurTab = getIntent().getIntExtra(ArgsConstants.INDEX,0);
        resetTab();
        mLl_ask.setOnClickListener(this);
        mLl_course.setOnClickListener(this);
        mLl_my.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_ask:
                mCurTab = 0;
                break;
            case R.id.ll_course:
                mCurTab = 1;
                break;
            case R.id.ll_my:
                mCurTab = 2;
                break;
        }
        resetTab();
    }

    /**
     * 根据mCurTab切换Fragment并重置TAB显示状态
     */
    private void resetTab() {
        BaseFragment fragment = HomeFragmentFactory.getFragment(mCurTab);
        if(fragment!=null) {
            //通过FragmentTransaction的hide和show方法，控制fragment的切换，这样可以保存fragment的状态。
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            hideOtherFragment(mCurTab, transaction);
            if(!fragment.isAdded()) {
                transaction.add(R.id.fl_content,fragment);
            }
            transaction.show(fragment);
            transaction.commit();
        }

        for(int i=0;i<HomeFragmentFactory.FRAGMENT_COUNT;i++) {
            if(mCurTab == i) {
                mIvList.get(i).setImageResource(mSelectedImg[i]);
                mTvList.get(i).setTextColor(getResources().getColor(R.color.text_tab_selected));
            } else {
                mIvList.get(i).setImageResource(mUnSelectedImg[i]);
                mTvList.get(i).setTextColor(getResources().getColor(R.color.text_tab_unselected));
            }
        }
    }

    /**
     * 隐藏mCurTab以外的fragment
     */
    private void hideOtherFragment(int mCurTab,FragmentTransaction transaction) {
        for(int i=0;i<HomeFragmentFactory.FRAGMENT_COUNT;i++) {
            if(i!=mCurTab) {
                BaseFragment fragment = HomeFragmentFactory.getFragment(i);
                if(fragment!=null) {
                    transaction.hide(fragment);
                }
            }
        }
    }

    /**
     * app运行在后台，Activity可能会被系统回收，而Fragment并不随着Activity回收而回收。
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        LogUtil.showInfoLog("onSaveInstanceState","onSaveInstanceState");
    }
}
